package com.lecture.java8.zip;

import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
 * @Description 压缩文件夹
 * @Author luyn
 * @Date 2020/7/23 10:28
 * @version 1.0
 */
public class CompressDirectory {
    /**
     * @author luyn
     * @description 一次性压缩多个文件到zip中
     * @date 10:30 2020/7/23
     * @param filePaths 需要压缩的文件地址列表（绝对路径）
     * @param zipFilePath 需要压缩到哪个zip文件（无需创建这样一个zip，只需要指定一个全路径）
     * @param keepDirStructure 压缩后目录是否保持原目录结构
     * @return int
     */
    public static int compress(List<String> filePaths, String zipFilePath, Boolean keepDirStructure, String fileCatalog) throws IOException{
        byte[] buf = new byte[1024];
        File zipFile = new File(zipFilePath);
        //zip文件不存在，则创建文件，用于压缩
        /*if(!zipFile.exists())
            zipFile.createNewFile();*/
        int fileCount = 0;//记录压缩了几个文件？
        try {
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile));
            for (String relativePath : filePaths) {
                if (StringUtils.isEmpty(relativePath)) {
                    continue;
                }
                File sourceFile = new File(relativePath);//绝对路径找到file
                if (!sourceFile.exists()) {
                    continue;
                }

                 FileInputStream fis = new FileInputStream(sourceFile);
                // 保持文件目录结构 相对结构路径
                zos.putNextEntry(new ZipEntry(relativePath.replace(fileCatalog, "")));
                /*if (keepDirStructure != null && keepDirStructure) {
                    //保持目录结构
                    zos.putNextEntry(new ZipEntry(relativePath));
                } else {
                    //直接放到压缩包的根目录
                    zos.putNextEntry(new ZipEntry(sourceFile.getName()));
                }*/
                //System.out.println("压缩当前文件："+sourceFile.getName());
                int len;
                while ((len = fis.read(buf)) > 0) {
                    zos.write(buf, 0, len);
                }
                zos.closeEntry();
                fis.close();
                fileCount++;
            }
            zos.close();
            //System.out.println("压缩完成");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileCount;
    }

    public static void main(String[] args) throws IOException {
        /*List<String> sourceFilePaths = new ArrayList<>();
        sourceFilePaths.add("F:\\Microservices\\zip\\");
        sourceFilePaths.add("F:\\Microservices\\zip\\zookeeper.properties");
        sourceFilePaths.add("F:\\Microservices\\zip\\1\\server.properties");
        //指定打包到哪个zip（绝对路径）
        String zipTempFilePath = "F:\\Microservices\\zip\\test.zip";

        // 第一层要压缩的文件目录
        String fileCatalog = "F:\\Microservices\\zip";
        //调用压缩
        int s = compress(sourceFilePaths, zipTempFilePath,true, fileCatalog);
        System.out.println("成功压缩"+s+"个文件");*/
        List<String> fileDirectory = new ArrayList<>();
        fileDirectory = CompressDirectory.getFileDirectory(new File("F:\\Microservices\\zip"), fileDirectory);
        fileDirectory.forEach(System.out::println);

    }


    public static List<String> getFileDirectory(File file, List<String> list) {
        if (file.isFile()) {
            list.add(file.getAbsolutePath());
        } else {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                CompressDirectory.getFileDirectory(files[i], list);
            }
        }
        return list;
    }
}
